<script>
import { useAppStore, useUserStore } from "@/store/modules";
import { getSiteInfo } from "@/api/home";
import { getToken } from "@/utils/auth";
import { doTaskGetReward } from "@/api/painting";
import Toast from "@vant/weapp/dist/toast/toast";
import { getIsOpen } from "@/utils/site";
let siteInfo = null;
let appStore = null;
function checkUpgrade() {
  const updateManager = wx.getUpdateManager();

  updateManager.onCheckForUpdate(function (res) {
    // 请求完新版本信息的回调

    if (res.hasUpdate) {
      updateManager.onUpdateReady(function () {
        wx.showModal({
          title: "更新提示",
          content: "新版本已经准备好，是否重启应用？",
          success(res) {
            if (res.confirm) {
              // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
              updateManager.applyUpdate();
            }
          },
        });
      });

      updateManager.onUpdateFailed(function () {
        // 新版本下载失败
      });
    } else {
    }
  });
}

const doTaskGetRewardFn = async () => {
  uni.removeStorageSync("doTaskGetReward");
  const res = await doTaskGetReward();
  Toast.success(res.message);
  useUserStore().getUserInfo();
};
function checkSiteIsOpen(data) {
  let isUpgrade = getIsOpen(data);
  if (isUpgrade) {
    uni.reLaunch({
      url: "/user-center/upgrade/index",
    });
    // uni.navigateTo({
    //   url: '/user-center/upgrade/index'
    // })
  }
}
async function getSiteInfoFn() {
  appStore = useAppStore();
  const { data } = await getSiteInfo();
  // data.upgradeEndTime = '2024-03-08 14:14:13'
  data.name = data.siteName;

  appStore.setSiteInfo(data);

  // userStore.getUserInfo();
  // localStorage.setItem('siteInfo', JSON.stringify(data))
  uni.setStorageSync("siteInfo", data);
  wx.setStorageSync("collectApp", true);
  checkSiteIsOpen(data);
}
export default {
  onLaunch: function (e) {
    if (e.query.inviteCode) {
      uni.setStorageSync("inviteCode", e.query.inviteCode);
    }
    checkUpgrade();
    let userStore = useUserStore();
    // getSiteInfoFn();
  },
  onShow: async function (e) {
    if (uni.getStorageSync("doTaskGetReward")) {
      doTaskGetRewardFn();
    }
    if (e.query.inviteCode) {
      uni.setStorageSync("inviteCode", e.query.inviteCode);
    }
    getSiteInfoFn();
  },
  onHide: function () { },
  //全局变量
  globalData: {
    _query: {},
    _isIOS: false,
  },
};
</script>

<style lang="less">
/*每个页面公共css */
page {
  background-color: var(--a-main-bg-color);
  // height: 100vh;
  background-attachment: fixed;
  --button-primary-background-color: #83cd88;
}

::v-deep {
  view.expire-vip-modal-box {
    .van-popup {
      background-color: transparent;
      width: 549rpx;
      height: 651rpx;
    }
  }

  view.van-cell-group--inset {
    background-color: #fff;
    border-radius: 50rpx;
    margin-top: 30rpx;
  }

  view.van-icon-arrow-left {
    color: var(--a-main-navBar-leftIcon-color) !important;
  }

  view.van-nav-bar__title {
    color: var(--a-one-text-color) !important;
  }

  .van-nav-bar {
    background-color: var(--a-main-tabNav-bg-color) !important;
  }

  .van-nav-bar--fixed {
    z-index: 2000;
  }

  view.van-hairline--bottom:after {
    border: none !important;
  }
}
</style>
